// 防抖
export function useDebounce<T>(value: T, delay = 300, callback: any) {
	let timer: any = null;
	return customRef((track, trigger) => {
		return {
			get() {
				track(); //告诉vue追踪value数据的变化
				return value;
			},
			set(newValue: T) {
				//使用clearTimeout和setTimeout来实现防抖动
				clearTimeout(timer);
				timer = setTimeout(() => {
					value = newValue;
					callback(newValue);
					trigger(); //告诉vue重新解析模板
				}, delay);
			}
		};
	});
}
// 节流
export function useThrottle<T>(value: T, delay = 300, callback: any) {
	let timer: any = null;
	return customRef((track, trigger) => {
		return {
			get() {
				track(); //告诉vue追踪value数据的变化
				return value;
			},
			set(newValue: T) {
				//使用clearTimeout和setTimeout来实现防抖动
				if (timer) return;
				timer = setInterval(() => {
					clearInterval(timer);
					value = newValue;
					callback(newValue);
					trigger(); //告诉vue重新解析模板
				}, delay);
			}
		};
	});
}
